package com.zz.word;


import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.policy.DynamicTableRenderPolicy;
import com.deepoove.poi.policy.TableRenderPolicy;
import com.deepoove.poi.util.TableTools;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 年度销售指标确认书
 */
public class SalePolicy extends DynamicTableRenderPolicy {

    @Override
    public void render(XWPFTable table, Object data) throws Exception {

        if (null == data) return;
        SaleTableData detailData = (SaleTableData) data;

        // 销售指标 填充数据所在行数,第一行是0
        int goodsStartRow = 2;
        // 总计 填充数据所在行数
        int sumRow = detailData.getSales().size() + goodsStartRow;
        // 总列数
        int columnSum = 15;

        List<RowRenderData> sales = detailData.getSales();
        if (sales.size() > 0) {
            table.removeRow(goodsStartRow);

            List<Map<String, Integer>> result = new ArrayList<>();
            for (int i = sales.size() - 1; i >= 0; i--) {
                XWPFTableRow insertNewTableRow = table.insertNewTableRow(2);
                // 统计 同一医院的数据条数
                for (int j = 0; j < columnSum; j++) insertNewTableRow.createCell();
                TableRenderPolicy.Helper.renderRow(table.getRow(goodsStartRow), sales.get(i));
            }

            List<XWPFTableRow> rows = table.getRows();

            if (rows.size() > 0) {
                // 表头两行
                String hospitalName = rows.get(2).getCell(0).getText();
                int fromRow = 0;
                int time = 0;

                for (int i = 2; i < rows.size(); i++) {
                    String tempName = rows.get(i).getCell(0).getText();
                    if (hospitalName.equals(tempName) && (i != rows.size() - 1)) {
                        if (time == 0) {
                            fromRow = i;
                        }
                        time += 1;
                    } else if (i == rows.size() - 1) {
                        int toRow = fromRow + time;
                        if (time >= 1 && toRow > fromRow) {
                            // 合并单元格，合并同一名称的医院名称
                            Map<String, Integer> map = new HashMap<>();
                            map.put("fromRow", fromRow);
                            map.put("toRow", toRow);
                            result.add(map);
                        }

                    } else {
                        int toRow = fromRow + time - 1;
                        if (time >= 1 && toRow > fromRow) {
                            // 合并单元格，合并同一名称的医院名称
                            Map<String, Integer> map = new HashMap<>();
                            map.put("fromRow", fromRow);
                            map.put("toRow", toRow);
                            result.add(map);
                        }
                        hospitalName = tempName;
                        fromRow = i;
                        time = 1;
                    }

                }
            }

            // 先渲染表格，再合并表格
            for (Map<String, Integer> map : result) {
                Integer fromRow1 = map.get("fromRow");
                Integer toRow1 = map.get("toRow");
                //System.out.println("fromRow1 = " + fromRow1);
                //System.out.println("toRow1 = " + toRow1);
                TableTools.mergeCellsVertically(table, 0, fromRow1, toRow1);
            }
        }

        List<RowRenderData> labors = detailData.getSums();
        if (labors.size() > 0) {
            table.removeRow(sumRow);
            // 循环插入行
            for (int i = 0; i < 1; i++) {
                XWPFTableRow insertNewTableRow = table.insertNewTableRow(sumRow);
                for (int j = 0; j < columnSum; j++) insertNewTableRow.createCell();
                TableRenderPolicy.Helper.renderRow(table.getRow(sumRow), labors.get(i));
                // 合并单元格，总计占2列
                TableTools.mergeCellsHorizonal(table, sumRow, 0, 1);
            }
        }
    }

}